package com.core.thread;

public class SaleTicket implements Runnable {
    int tickets = 100;

    @Override
    public void run() {
        while (tickets > 0) {
            try {
                sale();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // synchronized (this) {
            // if (tickets > 0) {
            // System.out.println(Thread.currentThread().getName() + "卖第"
            // + (100 - tickets + 1) + "张票");
            // tickets--;
            // }
            // }
        }
    }

    public synchronized void sale() throws InterruptedException {
        if (tickets > 0) {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "卖第" + (100 - tickets + 1) + "张票");
            tickets--;
        }

    }

}
